<template>
  <div>
    <FormPanel
      :list="formList"
      :cols="2"
      :labelWidth="110"
      @formChange="changeHandle"
    ></FormPanel>
    <BaseDialog
      :visible.sync="visible"
      :title="dialogTitle"
      destroyOnClose
      width="700"
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <dealer :type="handleType" :rowData="formData" @close="closeHandler" @reAdd='reAdd1'/>
    </BaseDialog>
  </div>
</template>

<script>
import dealer from './dealer/dealer';
import { dictionary } from '@/utils/dictMixin';
import { addBusinessCustomer,customersSelect } from '@/api/part/partInfoManage/contactsCustomer';
export default {
  mixins: [dictionary],
  components:{
    dealer
  },
  data() {
    this.formData = {}
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      formList: this.createTopFilterList(),
      fetchParams: {},
      visible:false,
      handleType:'',
      dialogTitle:''
    };
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '客户代码',
          fieldName: 'customerCode'
        },
        {
          type: 'SELECT',
          label: '客户类型名称',
          fieldName: 'customerTypeCode',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          selfCols:2
        },
        {
          type: 'INPUT',
          label: '客户拼音',
          fieldName: 'customerSpell'
        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'dealerShortName',
          unitRender: () => {
            return (
              <el-button size="mini" type="text" onClick={() => this.searchDealer()}>搜索</el-button>
            )
          }
        },
        {
          type: 'INPUT',
          label: '客户简称',
          fieldName: 'customerShortName'
        },
        {
          type: 'INPUT',
          label: '邮编',
          fieldName: 'zipCode'
        },
        {
          type: 'INPUT',
          label: '地址',
          fieldName: 'address',
          selfCols:2
        },
        {
          type: 'INPUT',
          label: '联系人',
          fieldName: 'contactorName'
        },
        {
          type: 'INPUT',
          label: '联系人电话',
          fieldName: 'contactorPhone'
        },
        {
          type: 'INPUT',
          label: '传真',
          fieldName: 'fax'
        },
        {
          type: 'INPUT',
          label: '联系人手机',
          fieldName: 'contactorMobile'
        },
        {
          type: 'INPUT',
          label: '合约编号',
          fieldName: 'contractNo'
        },
        {
          type: 'INPUT',
          label: '客户收款类别',
          fieldName: 'cusReceiveSort',
          itemList: [{value:'1',text:'客户付费'},{value:'2',text:'保险付费'},
                  {value:'3',text:'OEM索赔'},{value:'4',text:'4S点付费'},{value:'5',text:'其他索赔'}]
        },
        {
          type: 'INPUT',
          label: '签约开始日期',
          fieldName: 'agreementBeginDate'
        },
        {
          type: 'INPUT',
          label: '签约结束日期',
          fieldName: 'agreementEndDate'
        },
        {
          type: 'INPUT',
          label: '订货周期',
          fieldName: 'leadTime'
        },
        {
          type: 'INPUT',
          label: '价格系数',
          fieldName: 'priceRate'
        },
        {
          type: 'INPUT',
          label: '销售基价',
          fieldName: 'salesBasePriceType'
        },
        {
          type: 'INPUT',
          label: '维修欠款',
          fieldName: 'arrearageAmount'
        },
        {
          type: 'INPUT',
          label: '账龄',
          fieldName: 'accountAge'
        },
        {
          type: 'INPUT',
          label: '加价率',
          fieldName: 'priceAddRate'
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'oemTag',
          itemList:this.createDictList('1045')
        },
        {
          type: 'INPUT',
          label: '税号',
          fieldName: 'taxNo'
        }
      ];
    },
    searchDealer(){
      this.visible = true;
    },
    async changeHandle(val) {
      console.log(val);
      this.fetchParams = Object.assign({}, val);
      let res = await addBusinessCustomer(this.fetchParams);
      console.log(res);
      this.$emit('reAdd', 0, res);
    },
    async customersSelect(){
    },
    closeHandler(){

    },
    reAdd1(x, y){
      console.log(x, y);
        for(let key in y){
            for(let i = 0; i < this.formList.length; i++){
                if(this.formList[i].fieldName == 'dealerShortName'){
                     this.formList[i].initialValue = y['dealerShortName'];
                }
            }
        }
        this.visible = false;
    },
    async customersSelect(){
      let res = await customersSelect();
      let json = res.data.rows.map( s => ({
        value:s.customerTypeCode,
        text: s.customerTypeCode
      }));
      console.log(json)
      for(let j = 0; j < this.formList.length; j++){
        if(this.formList[j].fieldName == 'customerTypeCode'){
          this.formList[j].itemList = json;
          return;
        }
      }
    }
  },
  mounted() {
    this.customersSelect();
  }
};
</script>

<style scoped>
</style>
